<?php
// +----------------------------------------------------------------------
// | 功能说明"
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

class SysRoleModel extends Model
{
    
    protected $name = 'sys_role';
    protected $autoWriteTimestamp = true;//时间自动更新 create_time ,update_time


    public $title = '名称';



   
    public function edit($param)
    {
        try{
          
            $result =  $this->update($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('sysrole/index'), '修改角色成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


    public function add($param)
    {
        try{
          
            $result =  $this->save($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('sysrole/index'), '添加角色成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    public function getById($id)
    {
        return $this->where('id', $id)->find();
    }
    
    public function delById($id)
    {
        try{
            $this->where('id', $id)->update(['isdelete'=>'1']);
            return msg(1, '', '删除成功');
        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }


    /**
     * 获取所有列表
     */
    public function getAllList()
    {
        return $this->where('isdelete', 0)->select();
    }

    /**********************************LAYUI 基础版本修改 开始***********************************/


    /**
     * 获取角色数据
     * @param $param
     * @return mixed
     */
    public function getSysRole($param){
        if(!empty($param['page'])){
            $page = $param['page'];
            $limit = $param['limit'];
            if($param['rolename']){
                $rolename = $param['rolename'];
                $where = "isdelete=0 and rolename like '%".$rolename."%' ";
            }else{
                $where = "isdelete=0 ";
            }
            $result = self::where($where)->page($page)->limit($limit)->select();
        }else{
            $where = "isdelete=0 ";
            $result = self::where($where)->select();
        }

        $count = self::where($where)->count();
        if($result){
            foreach($result as $key=>$user){
                $list[] = $user->toArray();
            }

            $ret['data'] = $list;
            $ret['code'] = 0;
            $ret['msg']  = '获取数据成功';
            $ret['count'] = $count;
        }else{
            $ret['data'] = '';
            $ret['code'] = -1;
            $ret['msg']  = '获取数据失败';
            $ret['count'] = 0;
        }
        return $ret;
    }

    /**
     * 权限设置 获取权限列表
     * @param $roleid
     * @return mixed
     */
    public function getRoleList(){
        $model = new SysMenuModel();

        $count  = $model->where("isdelete=0 and parentid=0")->count();//分页计算总数（parentid=0）
        $result = $model->where("isdelete=0")->order('ordernum asc')->select();//

        if($result){
            $list = $this->getTree($result, 0);
            $ret['data'] = $list;
            $ret['code'] = 0;
            $ret['msg']  = '获取权限数据成功';
            $ret['count'] = $count;
        }else{
            $ret['data'] = '';
            $ret['code'] = -1;
            $ret['msg']  = '获取权限数据为空';
            $ret['count'] = 0;
        }


        return $ret;
    }

    /**
     * 菜单处理成树结构
     * @param $data
     * @param $pId
     * @return array
     */
    public function getTree($data, $pId)
    {
        $tree = array();
        foreach($data as $k => $v)
        {

            if($v->data['parentid'] == $pId)
            {        //父亲找到儿子

                $v->data['child'] = $this->getTree($data, $v->data['id']);
                $tree[] = $v->data;
                unset($data[$k]);
            }
        }

        return $tree;
    }

    /**
     * 保存角色
     * @param $param
     * @return mixed
     */
    public function ajaxSaveData($param){
        //dump($param);die;
        $type = $param['type'];

        if($type!='del' && $type!='edit2'){
            $rolename = $param['rolename'];
            $where = "isdelete=0 and rolename='$rolename'";
            $result = $this->where($where)->find();//新增或者编辑之前，检查新用户名是否存在

            if($result){
                $data = $result->toArray();
                if($data['rolename']==$rolename){
                    $ret['data'] = '';
                    $ret['code'] = -1;
                    $ret['msg']  = '改角色已经存在，请重新添加';
                    return $ret;
                }

            }
        }

        if($type=='add'){

            //$param['create_time'] = time();
            $result = self::allowField(true)->save($param);
            if($result){
                $msg = "添加成功";
            }else{
                $msg = "添加失败";
            }
        }
        if($type=='edit' || $type=='edit2'){
            if($type=='edit2'){
                unset($param['create_time']);
            }
            $param['update_time'] = time();
            $result = self::allowField(true)->save($param,['id'=>$param['id']]);
            if($result){
                $msg = "编辑成功";
            }else{
                $msg = "编辑失败";
            }
        }
        if($type=='del'){
            $data['id'] = $param['id'];
            $param['isdelete'] = 1;
            //$param['update_time'] = time();
            $result = self::allowField(true)->save($param,['id'=>$data['id']]);
            if($result){
                $msg = "删除成功";
            }else{
                $msg = "删除失败";
            }
        }

        if($result){
            $ret['data'] = '';
            $ret['code'] = 1;
            $ret['msg']  = $msg;
        }else{
            $ret['data'] = '';
            $ret['code'] = -1;
            $ret['msg']  = $msg;
        }
        return $ret;
    }

}
